﻿using System;
using System.Collections.Generic;
using System.Linq;
using Google.Contacts;
using Google.GData.Client;
using Luna.Data;
using Luna.Data.Configuration;
using Luna.Data.CRM;

namespace Luna.Connectors.Google
{
    public class GoogleContactProvider : LunaDataProvider
    {
        public GoogleContactProvider()
        {
            // service.setUserCredentials("leilalepole@gmail.com", "lepole93");
            // service.setUserCredentials("anolige@gmail.com", "musicislife5632");
        }

        public GoogleSettingsManager Manager { get; set; }

        public void AccountDelete(IReferenceData accountID)
        {
            throw new NotImplementedException();
        }

        public void AccountDelete(IEnumerable<IAccountData> accounts)
        {
            throw new NotImplementedException();
        }

        public IAccountData AccountGet(IReferenceData accountID)
        {
            throw new NotImplementedException();
        }

        public IEnumerable<IAccountData> AccountGetAll()
        {
            throw new NotImplementedException();
        }

        public void AccountSave(IAccountData account)
        {
            throw new NotImplementedException();
        }

        public void AccountSave(IEnumerable<IAccountData> accounts)
        {
            throw new NotImplementedException();
        }

        public void CategoryDelete(IReferenceData categoryID)
        {
            throw new NotImplementedException();
        }

        public ICategoryData CategoryGet(IReferenceData categoryID)
        {
            throw new NotImplementedException();
        }

        public IEnumerable<ICategoryData> CategoryGetAll()
        {
            ContactsRequest request = new ContactsRequest(Manager.Settings);
            Feed<Group> feed = request.GetGroups();
            foreach (Group entry in feed.Entries.Where(x => string.IsNullOrEmpty(x.SystemGroup)))
            {
                yield return CreateGroup(entry);
            }
        }

        public void CategorySave(ICategoryData category)
        {
            throw new NotImplementedException();
        }

        public void ContactDelete(IReferenceData contact)
        {
            throw new NotImplementedException();
        }

        public void ContactDelete(IEnumerable<IContactData> contact)
        {
            throw new NotImplementedException();
        }

        public IContactData ContactGet(IReferenceData contactID)
        {
            throw new NotImplementedException();
        }

        public IEnumerable<IContactData> ContactGetAll()
        {
            ContactsRequest request = new ContactsRequest(Manager.Settings);
            Feed<Contact> feed = request.GetContacts();
            foreach (Contact entry in feed.Entries)
            {
                yield return Create(entry);
            }
        }

        public void ContactSave(IContactData contact)
        {
            throw new NotImplementedException();
        }

        public void ContactSave(IEnumerable<IContactData> contacts)
        {
            throw new NotImplementedException();
        }

        public string Test(IGoogleSettings data)
        {
            try
            {
                ContactsRequest request = new ContactsRequest(Manager.TestQuery(data));
                Feed<Contact> feed = request.GetContacts();
                feed.Entries.Count();
                return string.Empty;
            }
            catch (Exception buffer)
            {
                Log.Error(buffer.Message, buffer);
                return buffer.Message;
            }
        }

        private IContactData Create(Contact entry)
        {
            var buffer = new ContactData();
            if (entry.Name != null)
            {
                buffer.Name = entry.Name.FamilyName;
                buffer.GivenName = entry.Name.GivenName;
            }

            if (entry.PrimaryEmail != null)
            {
                buffer.Mail = entry.PrimaryEmail.Address;
            }
            else if (entry.Emails.Count() > 0)
            {
                buffer.Mail = entry.Emails[0].Address;
            }

            var phoneNumber = entry.Phonenumbers.FirstOrDefault();
            if (phoneNumber != null)
            {
                buffer.PhoneNumber = phoneNumber.Value;
            }

            return buffer;
        }

        private ICategoryData CreateGroup(Group entry)
        {
            var buffer = new CategoryData();
            buffer.Name = entry.Title;
            buffer.Name = entry.Id.Substring(entry.Id.LastIndexOf("/") + 1);
            buffer.LastUpdate = entry.Updated;
            return buffer;
        }

        public void CategoryDelete(Guid categoryID)
        {
            throw new NotImplementedException();
        }
    }
}